package com.danbay.framework.kafka;

import com.danbay.framework.utils.StringUtils;
import lombok.Data;

/**
 * kafka接入服务包结构
 *
 * @author SJG
 *         2017/11/13.
 */
@Data
public class AccessPackage<T> {

    /**
     * 消息类型：请求
     */
    public static final byte MESSAGE_TYPE_REQUEST = 0x0;
    /**
     * 消息类型：应答
     */
    public static final byte MESSAGE_TYPE_RESPONSE = 0x1;

    /**
     * 消息头
     */
    private AccessHeader header = new AccessHeader();

    /**
     * 数据
     */
    private T data;

    /**
     * 是否为响应包
     *
     * @return boolean
     */
    public boolean isResponse() {
        return header.getType() == 1;
    }


    /**
     * 快捷设置消息头
     *
     * @param type      AccessHeader type
     * @param sequence  AccessHeader sequence
     * @param timestamp AccessHeader timestamp
     * @param source AccessHeader topicName
     */
    public void setHeaders(int type, long sequence, long timestamp, String source) {
        header.setType(type);
        header.setSequence(sequence);
        header.setTimestamp(timestamp);
        header.setTopicName(source);
    }

    @Override
    public String toString() {
        return StringUtils.format("header:{ver:{0},type:{1},sequence:{2},timestamp:{3},source topic:{4},data:{5}",
                header.getVersion(), header.getType(), header.getSequence(), header.getTimestamp(), header.getTopicName(), data);
    }

}
